﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using MyBlog.DataAccess;
using MyBlog.Model;
using MyBlog.Common;
using AutoMapper;
using MyBlog.BusinessInterface;
using MyBlog.DataEntity;
using MyBlog.DataAccessInterface;
namespace MyBlog.Business
{
    public class BusinessMessage : IBusinessMessage
    {
        IDALMessage _dalMessage;
        IUnitOfWork _unitWork;
        public BusinessMessage(IDALMessage dalMessage, IUnitOfWork unitWork)
        {
            _dalMessage = dalMessage;
            _unitWork = unitWork;
        }
        public void Add(MessageModel model)
        {
            var dataEntity = Mapper.Map<Tbl_Message>(model);
            _dalMessage.Add(dataEntity);
            _unitWork.Commit();
            model.ID = dataEntity.ID;

        }

        public void Update(MessageModel model)
        {
            var dataEntity = Mapper.Map<Tbl_Message>(model);
            _dalMessage.Update(dataEntity);
            _unitWork.Commit();
        }
        public void AddRange(IEnumerable<MessageModel> models)
        {
            var dataEntitys = Mapper.Map<List<Tbl_Message>>(models);
            _dalMessage.AddRange(dataEntitys);
            _unitWork.Commit();
        }

        public void DeleteRange(IEnumerable<MessageModel> models)
        {
            var dataEntitys = Mapper.Map<List<Tbl_Message>>(models);
            _dalMessage.DeleteRange(dataEntitys);
            _unitWork.Commit();
        }

        public List<MessageModel> GetAll(bool isTracking = true)
        {
            var entities = _dalMessage.GetAll(isTracking);
            return Mapper.Map<List<MessageModel>>(entities);
        }

        public void Delete(params object[] primaryKey)
        {
            _dalMessage.Delete(primaryKey);
            _unitWork.Commit();
        }

        public MessageModel Get(params object[] primaryKey)
        {
            var dataEntity = _dalMessage.Get(primaryKey);
            return Mapper.Map<MessageModel>(dataEntity);
        }

        public void Delete(MessageModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_Message>();
            if (model.ID > 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            _dalMessage.Delete(where);
            _unitWork.Commit();
        }

        public MessageModel Get(MessageModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_Message>();
            if (model.ID != 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            if (!string.IsNullOrWhiteSpace(model.AccountId))
            {
                where = where.And(x => x.AccountId.ToLower() == model.AccountId.ToLower());
            }
            var dataEntity = _dalMessage.Get(where);
            return Mapper.Map<MessageModel>(dataEntity);
        }

        public List<MessageModel> GetAll(MessageModel model, bool isTracking = true)
        {
            var where = LinqExpressionBuilder.True<Tbl_Message>();
            if (model.IsSystem.HasValue && model.IsSystem.Value)
            {
                where = where.And(x => x.AccountId == null || x.AccountId == "");
            }
            if (!string.IsNullOrWhiteSpace(model.AccountId))
            {
                where = where.And(x => x.AccountId.ToLower() == model.AccountId.ToLower().Trim());
            }
            var entities = _dalMessage.GetAll(where, isTracking);
            return Mapper.Map<List<MessageModel>>(entities);
        }


        public List<MessageModel> GetAll(MessageModel model, int page, int size, out int total, bool isTracking = true)
        {
            throw new NotImplementedException();
        }

        public void AddAgree(int id, string account)
        {
            var dataEntity = _dalMessage.Get(id);
            dataEntity.AgreeUsers = string.IsNullOrWhiteSpace(dataEntity.AgreeUsers) ? account : dataEntity.AgreeUsers + "&" + account;
            _unitWork.Commit();
        }
    }
}
